﻿#include "execonfig.h"

ExeConfig::ExeConfig()
{
    getzsqdef();
}
// 数据库读某一实验流程
QJsonArray ExeConfig::readsypz(int id)
{
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select step,type,value_1 From exe_config Where id='%1' and  step!=0  order by step ").arg(id);
    query.exec(sql);
    QStringList stepval;
    QStringList typeval;
    QStringList value_1val;
    QStringList colorval;
    QStringList pathval;
    QStringList idefval;
    stepval.append("");
    typeval.append("芯片孔位");
    value_1val.append("");
    colorval.append("#5BA55A");
    pathval.append("qrc:/picture/pz_xpkw.png");
    stepval.append("");
    typeval.append("开始");
    value_1val.append("");
    colorval.append("#BCA718");
    pathval.append("qrc:/picture/pz_ks.png");
    while (query.next()) {
        stepval.append(query.value("step").toString());
        if(query.value("type").toString()=="1"){
            typeval.append("注射器");
            colorval.append("#00417E");
            pathval.append("qrc:/picture/pz_zsq.png");
        }else if(query.value("type").toString()=="2"){
            typeval.append("阀门");
            colorval.append("#5BA58C");
            pathval.append("qrc:/picture/pz_fm.png");
        }else if(query.value("type").toString()=="3"){
            typeval.append("转盘");
            colorval.append("#735BA5");
            pathval.append("qrc:/picture/pz_zp.png");
        }else if(query.value("type").toString()=="4"){
            typeval.append("检测器");
            colorval.append("#5B66A4");
            pathval.append("qrc:/picture/pz_jc.png");
        }else if(query.value("type").toString()=="5"){
            typeval.append("等待");
            colorval.append("#FFB1D8");
            pathval.append("qrc:/picture/pz_dd.png");
        }else if(query.value("type").toString()=="6"){
            typeval.append("循环");
            colorval.append("#F0ABFF");
            pathval.append("qrc:/picture/pz_xh.png");
        }else if(query.value("type").toString()=="7"){
            typeval.append("跳过");
            colorval.append("#F3B46F");
            pathval.append("qrc:/picture/pz_tg.png");
        }else if(query.value("type").toString()=="8"){
            typeval.append("盖盖");
            colorval.append("#9B7E47");
            pathval.append("qrc:/picture/gg.png");
        }
        //        else if(query.value("type").toString()=="9"){
        //            typeval.append("气泵");
        //            colorval.append("#89439B");
        //            pathval.append("qrc:/picture/qb.png");
        //        }
        value_1val.append(query.value("value_1").toString());
    }
    stepval.append("");
    typeval.append("结束");
    value_1val.append("");
    colorval.append("#A55B80");
    pathval.append("qrc:/picture/pz_js.png");
    QJsonArray buttonDataArray;
    QJsonObject rec;
    for(int i = 0; i < stepval.size(); i++) {
        rec.insert("title", typeval[i]);
        rec.insert("icon", pathval[i]);
        rec.insert("color", colorval[i]);
        rec.insert("textval", value_1val[i]);
        rec.insert("numval", stepval[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
// 获取默认
int ExeConfig::getdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select isdef From exe_config Where id='%1' and  step=0  ").arg(id);
    query.exec(sql);
    while (query.next()) {
        int a = query.value("isdef").toInt();
        return a;
    }
    return 0;
}
// 设置默认
void ExeConfig::setdef(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Update exe_config set isdef = 0 ";
    query.exec(sql);
    sql = QString("Update exe_config set isdef = 1 where id='%1' ").arg(id);
    query.exec(sql);
    emit updef();
    return;
}
// 前端写实验信息
void ExeConfig::wsyxx(QString id,QString name,QString type,QString path){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    type=common->getItemCode("3",type);
    if(true){
        QString sql = QString("select value_1 from exe_config where id!='%4' and type=0 ").arg(id);
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value_1").toString());
        }
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit rename();
                return;
            }
        }
    }
    QString sql=QString("Update exe_config set value_1='%1',value_2='%2',value_3='%3' Where id='%4' and type=0  ").arg(name,type,path,id);
    query.exec(sql);
    emit updatatab(id.toInt(),name,type,path);
    return;
}
// 前端读实验名称
QString ExeConfig::rsyxx(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3 From exe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
    }
    qDebug()<<res[0];
    return res[0];
}
// 前端读实验名称
QString ExeConfig::rsyty(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3 From exe_config Where id='%1' and  step=0 ").arg(id);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        res.append(query.value("value_1").toString());
        res.append(query.value("value_2").toString());
        res.append(query.value("value_3").toString());
    }
    res[1]=common->getItemName("3",res[1]);
    qDebug()<<res[1];
    return res[1];
}
// 前端读取实验信息列表
QJsonArray ExeConfig::readsypzlist(QString name){
    if(name==""){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From exe_config Where step=0 order by id";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        QJsonArray buttonDataArray;
        QJsonObject rec;
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }else{
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id,value_1,value_2 From exe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        QStringList namelist;
        QStringList tyepelist;
        while (query.next()) {
            syid.append(query.value("id").toInt());
            namelist.append(query.value("value_1").toString());
            tyepelist.append(query.value("value_2").toString());
        }
        for(int i=syid.size()-1;i>=0;i--){
            if(!namelist[i].contains(name)){
                syid.removeAt(i);
                namelist.removeAt(i);
                tyepelist.removeAt(i);
            }
        }

        QJsonArray buttonDataArray;
        QJsonObject rec;
        for(int i = 0; i < syid.size(); ++i) {
            rec.insert("syid", syid[i]);
            rec.insert("syname", namelist[i]);
            rec.insert("sytype", tyepelist[i]);
            buttonDataArray.append(rec);
        }
        return buttonDataArray;
    }

}
// 前端新建实验信息
void ExeConfig::newsypz(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select id From exe_config Where step=0 ";
    query.exec(sql);
    QList<int> syid;
    while (query.next()) {
        syid.append(query.value("id").toInt());
    }
    int newid;
    for(int i=1;i<100;i++){
        if(!syid.contains(i)){
            newid=i;
            break;
        }
    }
    //    int randomNumber = QRandomGenerator::global()->bounded(100);
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    // 将当前日期时间按照指定格式转换为字符串
    QString currentDateTimeString = currentDateTime.toString(format);
    QString newname = currentDateTimeString;
    sql="Select value From Deftab Where id=3 and deffl=1 ";
    query.exec(sql);
    QString deftype;
    while (query.next()) {
        deftype=query.value("value").toString();
    }
    deftype=common->getItemCode("3",deftype);
    sql=QString("INSERT INTO exe_config ( id, isdef, step, type, value_1, value_2, value_3 ) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5', '%6', '%7' );").arg(QString::number(newid),"0","0","0",newname,deftype,"qrc:/picture/pz_xp1.png");
    query.exec(sql);
    shunxunumb++;
    emit addlist();
    return;
}
// 前端复制实验信息
void ExeConfig::copyypz(QString copyid){
    if(copyid!="0"){
        QSqlDatabase db = dbHelper::openConnection();
        QSqlQuery query(db);
        QString sql="Select id From exe_config Where step=0 ";
        query.exec(sql);
        QList<int> syid;
        while (query.next()) {
            syid.append(query.value("id").toInt());
        }
        int newid;
        for(int i=1;i<100;i++){
            if(!syid.contains(i)){
                newid=i;
                break;
            }
        }
        sql=QString("Insert into exe_config select '%1',0,step,type,value_1,"
                    "'','','','','','','','' from exe_config where id = '%2' ").arg(QString::number(newid),copyid);
        query.exec(sql);
        int randomNumber = QRandomGenerator::global()->bounded(100);
        QString newname = "实验名称"+QString::number(randomNumber);
        sql=QString("Update exe_config set value_1='%1' Where id='%2' and type=0").arg(newname,QString::number(newid));
        query.exec(sql);
        emit addlist();
        return;
    }

}
// 前端删除实验信息
void ExeConfig::delsypz(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("delete From exe_config Where id = '%1' ").arg(QString::number(id));
    query.exec(sql);
    sql = QString("UPDATE exe_config SET id = id - 1 WHERE id >'%1' ").arg(id);
    query.exec(sql);
    emit dellist();
    return;
}
// 前端更改信息
void ExeConfig::upall(QString id,QString numv,QString v1,QString v2,QString v3,QString v4,QString v5,QString v6,QString v7,QString v8,QString v9){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update exe_config set value_1='%1',value_2='%2',value_3='%3',value_4='%4',value_5='%5',"
                        "value_6='%6',value_7='%7',value_8='%8',value_9='%9' Where id='%10' and step='%11' "
                        "").arg(v1,v2,v3,v4,v5,v6,v7,v8,v9,id,numv);
    query.exec(sql);
    sql = QString("Select type,value_1,value_2 From exe_config Where id='%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    QString kjlx,value2,value1;
    while (query.next()){
        kjlx=query.value("type").toString();
        value1=query.value("value_1").toString();
        value2=query.value("value_2").toString();
    }
    if(kjlx=="1"){
        sql=QString("Update exe_config set value_2='%1' Where id='%2' and type='%3' and value_1='%4' "
                    "").arg(value2,id,"1",value1);
        query.exec(sql);
    }
    emit uplist();
    emit save();
    return;
}
// 前端读控件信息
QStringList ExeConfig::readall(QString id,QString numv){
    QStringList alldata;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 From exe_config Where id='%1' and  step='%2' ").arg(id,numv);
    query.exec(sql);
    QStringList res;
    while (query.next()){
        alldata.append(query.value("value_1").toString());
        alldata.append(query.value("value_2").toString());
        alldata.append(query.value("value_3").toString());
        alldata.append(query.value("value_4").toString());
        alldata.append(query.value("value_5").toString());
        alldata.append(query.value("value_6").toString());
        alldata.append(query.value("value_7").toString());
        alldata.append(query.value("value_8").toString());
        alldata.append(query.value("value_9").toString());
    }
    if(id=="1"&&alldata[8].isEmpty()){
        alldata[8]="mL/Min";
    }
    return alldata;
}
// 删除某控件
void ExeConfig::delkj(QString id,QString numv){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("delete From exe_config Where id = '%1' and step='%2' ").arg(id,numv);
    query.exec(sql);
    sql = QString("UPDATE exe_config SET step = step - 1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    emit uplist();
}
// 添加某控件
void ExeConfig::addkj(QString id,QString numv,QString type){
    qDebug()<<type;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and step>'%2' ").arg(id,numv);
    query.exec(sql);
    if(type=="8"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','是',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="2"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','开启',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="4"){
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','大于',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }else if(type=="1"){
        sql=QString("Select value_2 From exe_config Where id='%1' and  step='%2' ").arg(id,"0");
        QString sytype;
        query.exec(sql);
        while (query.next()){
            sytype=query.value("value_2").toString();
        }
        QString lx=getSyringTypezsq(sytype,"1");
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','1',"
                    "'','%2','打液','','','','','mL/Min' from exe_config where id = '%3' and step='%4'  ").arg(type,lx,id,numv);
    }else{
        sql=QString("Insert into exe_config select id,isdef,step+1,'%1','1',"
                    "'','','','','','','','' from exe_config where id = '%2' and step='%3'  ").arg(type,id,numv);
    }

    query.exec(sql);
    emit uplist();
}
// 控件移动
void ExeConfig::kjchange(QString id,double mbwz,QString kswz){
    qDebug()<<mbwz;
    int sjwz = mbwz-66;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    qDebug()<<sjnum;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QStringList alldata;
    if(kswz.toInt()<sjnum){
        QString sql = QString("SELECT id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        while (query.next()){
            alldata.append(query.value("id").toString());
            alldata.append(query.value("isdef").toString());
            alldata.append(query.value("step").toString());
            alldata.append(query.value("type").toString());
            alldata.append(query.value("value_1").toString());
            alldata.append(query.value("value_2").toString());
            alldata.append(query.value("value_3").toString());
            alldata.append(query.value("value_4").toString());
            alldata.append(query.value("value_5").toString());
            alldata.append(query.value("value_6").toString());
            alldata.append(query.value("value_7").toString());
            alldata.append(query.value("value_8").toString());
            alldata.append(query.value("value_9").toString());
        }
        sql = QString("DELETE  FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        sql = QString("UPDATE exe_config SET step = step -1 WHERE id = '%1' and step>'%2' and step<='%3'").arg(id,kswz,QString::number(sjnum));
        query.exec(sql);
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,alldata[1],QString::number(sjnum),alldata[3]
                ,alldata[4],alldata[5],alldata[6],alldata[7],alldata[8],alldata[9],alldata[10],alldata[11],alldata[12]);
        query.exec(sql);
    }else if(kswz.toInt()>sjnum){
        sjnum+=1;
        QString sql = QString("SELECT id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9 FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        while (query.next()){
            alldata.append(query.value("id").toString());
            alldata.append(query.value("isdef").toString());
            alldata.append(query.value("step").toString());
            alldata.append(query.value("type").toString());
            alldata.append(query.value("value_1").toString());
            alldata.append(query.value("value_2").toString());
            alldata.append(query.value("value_3").toString());
            alldata.append(query.value("value_4").toString());
            alldata.append(query.value("value_5").toString());
            alldata.append(query.value("value_6").toString());
            alldata.append(query.value("value_7").toString());
            alldata.append(query.value("value_8").toString());
            alldata.append(query.value("value_9").toString());
        }
        sql = QString("DELETE  FROM exe_config where id='%1' and step='%2' ; ").arg(id,kswz);
        query.exec(sql);
        sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and step<'%2' and step>='%3'").arg(id,kswz,QString::number(sjnum));
        query.exec(sql);
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12');").arg(id,alldata[1],QString::number(sjnum),alldata[3]
                ,alldata[4],alldata[5],alldata[6],alldata[7],alldata[8],alldata[9],alldata[10],alldata[11],alldata[12]);
        query.exec(sql);
    }
}
// 控件移动添加
void ExeConfig::addkjchange(QString id,double mbwz,QString type){
    int sjwz = mbwz-64;
    int sx = sjwz%33;
    int sjnum;
    if(sx>16.5){
        sjnum = sjwz/33+1;
    }else{
        sjnum = sjwz/33;
    }
    if(sjnum<=0){
        return;
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    int maxStep;
    QString sql=QString("Select max(step) as Step From exe_config Where id='%1'").arg(id);
    query.exec(sql);
    while (query.next()) {
        maxStep=query.value("step").toInt();
    }
    if(maxStep<sjnum){
        sjnum=maxStep+1;
    }
    qDebug()<<"MaxStep:"+QString::number(maxStep);

    qDebug()<<QString::number(sjnum);
    sql = QString("UPDATE exe_config SET step = step +1 WHERE id = '%1' and step>='%2' ").arg(id,QString::number(sjnum));
    query.exec(sql);
    sql=QString("Select isdef From exe_config Where id='%1' and  step='0' ").arg(id);
    query.exec(sql);
    QString defv;
    while (query.next()){
        defv = query.value("isdef").toString();
    }
    if(type=="8"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"是","","","","","","","","");
    }else if(type=="2"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"开启","","","","","","","","");
    }else if(type=="4"){
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"大于","","","","","","","","");
    }else if(type=="1"){
        sql=QString("Select value_2 From exe_config Where id='%1' and  step='%2' ").arg(id,"0");
        QString sytype;
        query.exec(sql);
        while (query.next()){
            sytype=query.value("value_2").toString();
        }
        QString lx=getSyringTypezsq(sytype,"1");
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"1","",lx,"打液","","","","","mL/Min");
    }else{
        sql=QString("INSERT INTO exe_config (id,isdef,step,type,value_1,value_2,value_3,value_4,value_5,value_6,value_7,value_8,value_9) "
                    "VALUES ('%1','%2','%3','%4','%5','%6','%7','%8','%9','%10','%11','%12','%13');").arg(id,defv,QString::number(sjnum),type
                                                                                                          ,"1","","","","","","","","");
    }

    query.exec(sql);
    emit uplist();
}
// 默认信息读
QJsonArray ExeConfig::getdeff(int val){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select deffl,value,num,value_2 From Deftab Where id='%1' order by num").arg(QString::number(val));
    query.exec(sql);
    QStringList isdef;
    QStringList namelist;
    QStringList numlist;
    QStringList namelist2;
    while (query.next()) {
        isdef.append(query.value("deffl").toString());
        namelist.append(query.value("value").toString());
        numlist.append(query.value("num").toString());
        namelist2.append(query.value("value_2").toString());
    }
    QJsonArray buttonDataArray;
    QJsonObject rec;
    for(int i = 0; i < isdef.size(); ++i) {
        rec.insert("isdef", isdef[i]);
        rec.insert("name", namelist[i]);
        rec.insert("num", numlist[i]);
        rec.insert("namev", namelist2[i]);
        buttonDataArray.append(rec);
    }
    return buttonDataArray;
}
// 默认信息修改
void ExeConfig::updatadeff(int id,int num,QString name,QString namev){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    if(true){
        QString sql = QString("select value from Deftab where num!='%1' and id='%2' ").arg(QString::number(num),QString::number(id));
        query.exec(sql);
        QStringList res;
        while (query.next()){
            res.append(query.value("value").toString());
        }
        qDebug()<<name<<res;
        for(int i=0;i<res.length();i++){
            if(res[i]==name){
                emit defrename();
                return;
            }
        }
    }
    QString sql=QString("Update Deftab set value='%1',value_2='%2' Where id='%3' and num='%4' "
                        "").arg(name,namev,QString::number(id),QString::number(num));
    query.exec(sql);

    if(id==3){
        sql=QString("Update SyringToalInfo Set Num='%1' Where ConsumablesTypes='%2'").arg(namev,QString::number(num));
        query.exec(sql);
    }
    return;
}
// 默认信息添加
void ExeConfig::adddeff(int id){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select num From Deftab Where id='%1' ").arg(QString::number(id));
    query.exec(sql);
    QList<int> numlist;
    while (query.next()) {
        numlist.append(query.value("num").toInt());
    }
    int newid;
    for(int i=1;i<100;i++){
        if(!numlist.contains(i)){
            newid=i;
            break;
        }
    }
    sql=QString("INSERT INTO Deftab ( id, deffl, num, value ,value_2) "
                " VALUES ( '%1', '%2', '%3', '%4', '%5' );").arg(QString::number(id),"0",QString::number(newid),"","6");
    query.exec(sql);
    return;
}
// 默认信息删除
void ExeConfig::deldeff(int id,int num){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("delete From Deftab Where id = '%1' and num = '%2' ").arg(QString::number(id),QString::number(num));
    query.exec(sql);
    sql = QString("UPDATE Deftab SET num = num - 1 WHERE id = '%1' and num>'%2' ").arg(QString::number(id),QString::number(num));
    query.exec(sql);

    if(id==3){
        sql=QString("Delete From SyringeMaintenance Where ConsumablesTypes='%1'").arg(QString::number(num));
        query.exec(sql);

        sql=QString("Delete From SyringToalInfo Where ConsumablesTypes='%1'").arg(QString::number(num));
        query.exec(sql);

        sql=QString("Update SyringeMaintenance Set ConsumablesTypes=ConsumablesTypes-1 Where ConsumablesTypes>'%1'").arg(QString::number(num));
        query.exec(sql);

        sql=QString("Update SyringToalInfo Set ConsumablesTypes=ConsumablesTypes-1 Where ConsumablesTypes>'%1'").arg(QString::number(num));
        query.exec(sql);
    }

    return;
}
// 空默认信息删除
void ExeConfig::delnulldeff(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="delete From Deftab Where value = '' ";
    query.exec(sql);
}
// 默认信息设置
void ExeConfig::defset(int id,int num){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Update Deftab set deffl = 0 where id='%1' ").arg(QString::number(id));
    query.exec(sql);
    sql = QString("Update Deftab set deffl = 1 where id='%1' and num = '%2' ").arg(QString::number(id),QString::number(num));
    query.exec(sql);
    return;
}
// 注射器默认信息读取
QStringList ExeConfig::getzsqdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='1' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='1' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
// 盖盖默认信息读取
QStringList ExeConfig::getggdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='2' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='2' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
// 耗材类型信息读取
QStringList ExeConfig::gethclxdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='3' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='3' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}
// 转盘耗材类型信息读取
QStringList ExeConfig::zpgethclxdef(){
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select value From Deftab Where id='3' and deffl='1' ";
    query.exec(sql);
    QStringList namelist;
    namelist.append("");
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    sql="Select value From Deftab Where id='3' and deffl='0' ";
    query.exec(sql);
    while (query.next()) {
        namelist.append(query.value("value").toString());
    }
    return namelist;
}


Q_INVOKABLE QString ExeConfig::getSyringType(QString ConsumablesTypes,QString serial)
{
    QString code= common->getItemCode("3",ConsumablesTypes);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Type From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    QString result;
    while (query.next())
    {
        result=query.value("Type").toString();
    }
    return result;
}


Q_INVOKABLE QString ExeConfig::getSyringTypezsq(QString ConsumablesTypes,QString serial)
{
    QString code= ConsumablesTypes;
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select Type From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    QString result;
    while (query.next())
    {
        result=query.value("Type").toString();
    }
    return result;
}

Q_INVOKABLE int ExeConfig::getSyringeNumByConsumableName(QString consumableName){

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);

    int res = 0;
    QString sql=QString("Select value_2 from Deftab where value='%1'").arg(consumableName);
    query.exec(sql);
    while(query.next()){
        res = query.value("value_2").toInt();
    }

    return res;
}

Q_INVOKABLE QString ExeConfig::getDistance(QString ConsumablesTypes,QString serial,QString volume,QString deviation)
{
    QString code=common->getItemCode("3",ConsumablesTypes);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select InsideDiameter,PublicErrand From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    double InsideDiameter;
    double PublicErrand;
    while (query.next()) {
        InsideDiameter=query.value("InsideDiameter").toDouble();
        PublicErrand=query.value("PublicErrand").toString().isNull()?0:query.value("PublicErrand").toDouble();
    }

    double r=(InsideDiameter+PublicErrand)/2;
    double area=3.1416*r*r;
    double result=(volume.toDouble()/area)*1000+deviation.toDouble();

    return QString::number(result,'f',4);
}

Q_INVOKABLE QString ExeConfig::getVolume(QString ConsumablesTypes,QString serial,QString distance,QString deviation){
    QString code= common->getItemCode("3",ConsumablesTypes);
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select InsideDiameter,PublicErrand From SyringeMaintenance Where ConsumablesTypes='%1' and id='%2'").arg(code,serial);
    query.exec(sql);
    double InsideDiameter;
    double PublicErrand;
    while (query.next()) {
        InsideDiameter=query.value("InsideDiameter").toDouble();
        PublicErrand=query.value("PublicErrand").toString().isNull()?0:query.value("PublicErrand").toDouble();
    }
    double r=(InsideDiameter+PublicErrand)/2;
    double area=3.1416*r*r;
    double result=(distance.toDouble()+deviation.toDouble())*area/1000;

    return result==0?"":QString::number(result,'f',4);
}

Q_INVOKABLE void ExeConfig::resetAllSyringe(QString ConsumablesTypes){
    udpClient=UdpClient::getInstance();
    int code=common->getItemCode("3",ConsumablesTypes).toInt();
    QString info=QString("62 00 00 3C 00 07 08 %1 E7 2F 55 AA").arg(common->TenToSixteen(code,4));
    udpClient->ClientSendingData(common->GetSendData(info));
}

//导出
Q_INVOKABLE int ExeConfig::exportExeConfig(QString id)
{
    QString path="";
    QString  savePath = QFileDialog::getSaveFileName(NULL, "保存", path,"Wlk files(*.txt)");
    if(savePath.isEmpty()&&savePath.isNull()){
        return -1;
    }

    QFile file(savePath);
    file.open(QIODevice::Append);

    QTextStream out(&file);  // QTextStream会自动将 Unicode 编码同操作系统的编码进行转换
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    out.setCodec(codec);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql=QString("Select * From exe_config Where Id='%1'").arg(id);
    query.exec(sql);
    while (query.next())
    {
        QString addSql=QString("Insert into exe_config Values('%1','%2','%3','%4','%5','%6','%7','%8',"
                               "'%9','%10','%11','%12','%13')").arg(
                    query.value("id").toString(),"0",query.value("step").toString(),
                    query.value("type").toString(),query.value("value_1").toString(),
                    query.value("value_2").toString(),query.value("value_3").toString(),
                    query.value("value_4").toString(),query.value("value_5").toString(),
                    query.value("value_6").toString(),query.value("value_7").toString(),
                    query.value("value_8").toString(),query.value("value_9").toString());
        out<<addSql+"\n";
    }

    file.close();
    return 0;
}

//导入
Q_INVOKABLE int ExeConfig::importExeConfig()
{
    QStringList sfilePath;
    QFileDialog *fDialog=new QFileDialog(NULL,"file dlg","","Txt files(*.txt)");
    if(fDialog->exec()==QFileDialog::Accepted)
    {
        sfilePath = fDialog->selectedFiles();
    }
    if(sfilePath.length()<=0)   return 1;
    QString filepath = sfilePath[0];
    QFile file(filepath);
    if(!file.exists()){
        qDebug() << "文件不存在";
        return -1;
    }
    QTextCodec* codec = QTextCodec::codecForName("GBK");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream txtInput(&file);
    txtInput.setCodec(codec);

    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql="Select max(id)+1 as Serial From exe_config";
    query.exec(sql);
    QString serial;
    while (query.next()) {
        serial=query.value("Serial").toString().isNull()?"1":query.value("Serial").toString();
    }
    // 获取当前日期和时间
    QDateTime currentDateTime = QDateTime::currentDateTime();
    // 定义日期时间格式
    QString format = "yyyyMMddhhmmss";
    QString name= currentDateTime.toString(format);
    int i=0;
    while (!txtInput.atEnd())
    {
        QString lineStr = txtInput.readLine();
        if(!lineStr.contains("Insert into exe_config")){
            return -2;
        }
        QStringList strList = lineStr.split("'");
        strList[1]=serial;
        if(i==0){
            strList[9]=name;
            i++;
        }
        lineStr = strList.join("'");
        query.exec(lineStr);
    }
    db.commit();
    return 0;
}
